package dao;

import lombok.Data;
import org.springframework.jdbc.core.JdbcTemplate;
import pojo.User;
import pojo.User;

import java.util.List;
@Data
public class UserDaoImpl implements UserDao{
    private JdbcTemplate jdbcTemplate;
    @Override
    public int addUser(User user) {
        String sql = "insert into user (username,password,sex) values (?,?,?)";
        int update = jdbcTemplate.update(sql, user.getUsername(), user.getPassword(),user.getSex());
        return update;
    }

    @Override
    public int deleteUser(int id) {
        String sql="delete from user where id = ?";
        return jdbcTemplate.update(sql,id);
    }

    @Override
    public User findUser(int id) {
        String sql = "select * from user where id=?";
        return jdbcTemplate.queryForObject(sql,new UserRowMapper(),id);
    }

    @Override
    public List<User> findAllUser() {
        String sql = "select * from user";
        System.out.println(jdbcTemplate);
        return jdbcTemplate.query(sql,new UserRowMapper());
    }

    @Override
    public int updateUser(User user) {
        String sql = "update user set username=?,password=?,sex=? where id=?";
        Object[] args={user.getUsername(),user.getPassword(),user.getSex(),user.getId()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int getByUserName(String username) {
        String sql = "select * from user where username=?";
        return jdbcTemplate.query(sql,new UserRowMapper(),username).size();
    }
}
